import os
from tqdm import tqdm
import sys
import subprocess
import json

def json_write(path:str,key:str,value:str):
    key = str(key)
    path = str(path)
    def get_json_data():
        with open(path, 'rb') as f:
            params = json.load(f)
            params[key] = value
            print("写入配置文件信息：", params)
        return params
    def write_json_data(params):
        with open(path, 'w') as r:
            json.dump(params, r)
    the_revised_dict = get_json_data()
    write_json_data(the_revised_dict)

WorkSpace = sys.path[0]

def file_extension(path):
    return os.path.splitext(path)[1]

def scan_run(file_path:str):
    list_global = []
    list = os.listdir(file_path)
    for i in tqdm(range(0,len(list)),desc="扫描进度"):
        path = os.path.join(file_path, list[i])
        if os.path.isfile(path):
            if os.path.isfile(path):
                if file_extension(path) == ".ncm":
                    try:
                        list_global.append(path)
                    except:
                        pass
    return list_global

def run(path:str):
    json_write(WorkSpace+".\\modules\\ncmtomusic\\Global_List.json","list_global",list(scan_run(path)))
    path= WorkSpace+".\\modules\\ncmtomusic\\Global_Allocation.py"
    subprocess.Popen(['python.exe', path])